<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MSearchController
extends MApplicationComponent
implements MIController
{
    private $_root      = null;
    private $_user_id   = null;
    private $_locale    = null;
    
    public function invoke($uri=null)
    {
                                parent::init();
        $params = $_REQUEST;
                if (isset($params) === false || $params == null) {
            throw new MAuthorizationException(FXP::t('Invalid parameters'));
        }
        $url                = $uri;
        $parts              = array_slice(explode('/', $url), 3);
        $this->_root        = $parts[0];
        
        $path               = null;
        foreach ($parts as $key => $name)
        {
            if ($key===0)
            {
                                continue;
            }
            $path .= "/{$name}";
        }
                                if ($pos = strpos($this->_root, '?'))
        {
            $this->_root = substr($this->_root, 0, $pos);
        }
        if ($pos = strpos($path, '?'))
        {
            $path = substr($path, 0, $pos);
        }

                        $user   = MUserManager::getInstance()->getCurrentUser();
        $this->_user_id     = $user->user_id;
        
        $query              = "";
        if (isset($params["query"])) {
            $query          = $params["query"];
        }
        $file_limit         = 10000;
        if (isset($params["file_limit"])) {
            $file_limit     = $params["file_limit"];
        }
        $include_deleted    = false;         if (isset($params["include_deleted"])) {
            $include_deleted = $params["include_deleted"];
        }
        $this->_locale      = "bytes";
        if (isset($params["locale"])) {
            $this->_locale = $params["locale"];
        }
        $callback           = null;
        if (isset($params["callback"])) {
            $callback       = $params["callback "];
        }
        $path               = "/{$this->_user_id}{$path}";
        $path               = MUtils::convertStandardPath($path) . "/";
        if ($query === "")
        {
            throw new MFileopsException(
                                        FXP::t('bad request'),
                                        MConst::HTTP_CODE_400);
        }
                                $query_db_file = MFiles::searchFilesByPath($path, 
                                                   $query, 
                                                   $this->_user_id, 
                                                   $include_deleted);

        if (count($query_db_file) > $file_limit)
        {
            throw new MFileopsException(
                                        FXP::t('Too many file entries to return'),
                                        MConst::HTTP_CODE_406);
        }
        $response = array();
        foreach ($query_db_file as $key => $db_file)
        {
            $file_array = array();
            $mime_type = null;
            if ($db_file["file_type"] == MConst::OBJECT_TYPE_FILE)
            {
                $version = MFileVersions::queryFileVersionByID($db_file["version_id"]);
                if ($version)
                {
                    $mime_type = $version[0]["mime_type"];
                }
            }
            $file_array   = $this->assembleResponse($file_array, $db_file, $mime_type);
            array_push($response, $file_array);
        }
        echo json_encode($response);
    }

    
    private function assembleResponse($response, $file_detail, $mime_type)
    {
        $file_path                          = $file_detail["file_path"];
        $index                              = strlen("/{$this->_user_id}");
        $file_path                          = substr_replace($file_path,"",0, $index);
        $response["size"]                   = MUtils::getSizeByLocale($this->_locale, $file_detail["file_size"]);
        $response["bytes"]                  = $file_detail["file_size"];
        $response["path"]                   = $file_path;
        $response["modified"]               = MUtils::formatIntTime($file_detail["file_update_time"]);
        $response["rev"]                    = strval($file_detail["version_id"]);
        $response["revision"]               = $file_detail["version_id"];
        $response["root"]                   = $this->_root;
        $response["thumb_exists"]           = false;
        $is_dir = true;
        if ($file_detail["file_type"] == MConst::OBJECT_TYPE_FILE)
        {
            $is_dir = false;
        }
        $response["is_dir"]                 = $is_dir;
        if (!empty($mime_type))
        {
            $response["mime_type"]              = $mime_type;
        }
        return $response;
    }
}
?>